<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
    var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
 
        $mainframe = JFactory::getApplication();
 
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('com_frinformation.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        //$this->setState('limit', $limit);
        $this->setState('limit', 5);
        $this->setState('limitstart', $limitstart);
    }
	function getAll($list = null){
        
       $qr = "SELECT DISTINCT CAIN_CODE,CAIN_NAME
                    FROM cain INNER JOIN info ON CAIN_CODE=INFO_CAIN_CODE
                              INNER JOIN usin ON INFO_CODE=USIN_INFO_CODE
              ";
        $aut=new bgAuthorize();
        $group=$aut->getUserGroup();
        $u=JFactory::getUser();
        $u_id= $u->id;     
        $where = array();
        $cate_id= JRequest::getVar('c_cid');
        if ($cate_id !=null)
        {
            $where[]='CAIN_CODE='.$cate_id[0];
        }
        if ($group==11 || $group==12 || $group==10)
        {
            $where[]="USIN_USER_CODE=$u_id";
        }
        //$where[]="USIN_USER_CODE=$u_id";
        $where[]="INFO_FLAG=1";                
        if(($list['TITLE']) != null ){
            $where[] = "INFO_TITLE LIKE '%$list[TITLE]%'";
            }
        
          if(!empty($list['DATE'])){
                      $where[] = "date(INFO_DATE) = '$list[DATE]'";
          }
                  
                  
        //search
        if(($list['CAIN']) !=0 ){
                   $where[] = "CAIN_CODE =$list[CAIN]";  
        }           
   
       if($where){
            $where = ' WHERE '.implode(' AND ', $where);
        }else{
            $where = null;
        }
       // get total number record
       $this->_total = $this->getTotal($where);
         
       $qr .= $where;
       
       // order by
       $qr .= " ORDER BY CAIN_CODE DESC"; 
       
       // pagination
       $qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
       //echo $qr;
        //load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       //load data info
        foreach($result as $row)
            {
                $a= $row->CAIN_CODE;
                $qr = "SELECT INFO_TITLE,INFO_CODE,INFO_DATE,INFO_CAIN_CODE FROM  info INNER JOIN usin ON info.INFO_CODE=usin.USIN_INFO_CODE  ";
                $where = array();
                  //search
                  //get user id
                 
                 $where[]='USIN_USER_CODE='.$u_id;
                $user_id= JRequest::getVar('u_cid');
                if ($user_id !=null)
                {
                    $where[]='USIN_USER_CODE='.$user_id[0];
                }
                $where[]="INFO_CAIN_CODE=$a";
                $where[]="INFO_FLAG=1";                
                if(($list['TITLE']) != null ){
                    $where[] = "INFO_TITLE LIKE '%$list[TITLE]%'";
                    }
                
                  if(!empty($list['DATE'])){
                              $where[] = "date(INFO_DATE) = '$list[DATE]'";
                  }
                   if($where){
                        $where = ' WHERE '.implode(' AND ', $where);
                    }else{
                        $where = null;
                    }
                     
                   $qr .= $where;
                   
                   // order by
                   $qr .= " ORDER BY INFO_CODE DESC"; 
                   //echo $qr;
                   $this->_db->setQuery($qr);
                   $data = $this->_db->loadObjectList();
                   $row->data = $data;
                 
            }
        
       return $result;
	}
    // get total
    function getTotal($where = null){
        // Load the content if it doesn't already exist
        if ($this->_total === null) {  
            $query = "SELECT count(distinct CAIN_CODE)
                             FROM cain INNER JOIN info ON CAIN_CODE=INFO_CAIN_CODE
                                       INNER JOIN usin ON INFO_CODE=USIN_INFO_CODE
                                        $where";
           // echo $query;return;exit();
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        
        return $this->_total;
    } 
    
    function getPagination(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
        //load data user 
    function loadusin($cid){
        
        $query = "SELECT name,username,email
                        from kc2vr_users inner join usin on kc2vr_users.id=usin.USIN_USER_CODE
                    WHERE  USIN_INFO_CODE=($cid)";
        $this->_db->setQuery($query);
        $result = $this->_db->loadObjectList();
        return $result;
        
    }
    
    //load detail infomation
    function load($cid){
        
        $query = "SELECT DISTINCT CAIN_CODE,CAIN_NAME,INFO_TITLE,INFO_DATE,INFO_BODY,INFO_FILE_1,INFO_FILE_2,INFO_FILE_3,INFO_FILE_4,INFO_FILE_5
                        FROM cain INNER JOIN info ON cain.CAIN_CODE=info.INFO_CAIN_CODE
                    WHERE  INFO_CODE=($cid)";
        $this->_db->setQuery($query);
        $result = $this->_db->loadObject();
       // echo ($result->info_body);
        return $result;
        
    }
 
    //load all CAIN
    function getAllCain(){

	   $qr = "SELECT * FROM cain";
       // order by
       $qr .= " ORDER BY CAIN_CODE DESC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	} 

  
}
?>
